package com.haier.uos.springcloud.basic.util;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class XmlBuilder {
    private XmlBuilder() {}

    /**
     * xml转换为指定的POJO
     * @param clazz
     * @param xmlStr
     * @return
     * @throws JAXBException
     */
    public static Object xmlStrToObject(Class<?> clazz, String xmlStr) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Reader reader = new StringReader(xmlStr);
        Object xmlObject = unmarshaller.unmarshal(reader);
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return xmlObject;

    }
}
